package com.rs.game.player;

import java.util.TimerTask;

import com.rs.cores.CoresManager;
import com.rs.game.player.actions.Rest;

public class AFKManager {
	
	private transient Player player;
	private int time = 600;
	
	private TimerTask timer = new TimerTask() {
		
		@Override
		public void run() {
			if (player.hasFinished()) {
				cancel();
			}
			if (time == 300) {
				player.setAFK(true);
				player.getPackets().sendGameMessage("You are now AFK.");
				if (!player.isResting()) {
					player.getActionManager().setAction(new Rest());
				}
			} else if (time == 0) {
				player.getSession().getChannel().close();
			}
			if (time > 0) {
				time--;
			}
		}
	};
	
	public AFKManager(Player player) {
		this.player = player;
	}
	
	public void reset(Player player) {
		time = 600;
		if (player.isAFK()) {
			player.getPackets().sendGameMessage("You are no longer AFK.");
			player.setAFK(false);
		}
	}
	
	public static void kick(Player player) {
		player.getSession().getChannel().close();
	}
	
	public void startTimer() {
		CoresManager.fastExecutor.schedule(timer, 0L, 1000L);
	}
}